package edu.gyc.myshop.controller;


import edu.gyc.myshop.model.Myorder;
import edu.gyc.myshop.model.Myuser;
import edu.gyc.myshop.model.OrderDetail;
import edu.gyc.myshop.model.UserAddress;
import edu.gyc.myshop.service.MyorderService;
import edu.gyc.myshop.service.OrderDetailService;
import edu.gyc.myshop.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ls
 * @since 2024-04-20
 */
@Controller

public class MyorderController {
    @Autowired
    MyorderService orderService;

    @RequestMapping("toOrder")
    public String dealOrder(HttpSession session, Model model) {
        Myuser user = (Myuser) session.getAttribute("user");
        if (user != null) {
            orderService.putOrder(session);
            List<Myorder> orders = orderService.lambdaQuery()
                    .eq(Myorder::getUserid, user.getId())
                    .list();
            model.addAttribute("orders", orders);
            return "/order";
        }

        return "/";
    }


    @RequestMapping("/myOrder")
    public String myOrder(HttpSession session, Model model) {

        List<Myorder> orders = orderService.listOrder(session);

        String msg = "尚未提交订单！";
        if (orders != null && orders.size() > 0) {
            model.addAttribute("orders", orders);
            return "/order";
        } else {
            model.addAttribute("msg", msg);
            return "/";
        }


    }

    @Autowired
    UserAddressService userAddressService;

    @RequestMapping("/toPay")
    public String toPay(Integer orderId, Model model) {

        Myorder order = orderService.lambdaQuery()
                .eq(Myorder::getId, orderId)
                .one();

        int userId = order.getUserid();
        UserAddress address = userAddressService.lambdaQuery()
                .eq(UserAddress::getUserId, userId)
                .one();
        //freeamark取值，至少得有对象，对象的属性值可为空
        if (address == null) {
            address = new UserAddress();
        }
        model.addAttribute("order", order);
        model.addAttribute("address", address);

        return "/pay";


    }

    @RequestMapping("/pay")
    @Transactional
    public String pay(UserAddress userAddress, Integer orderId, Model model) {

        userAddressService.saveOrUpdate(userAddress);
        orderService.lambdaUpdate()
                .eq(Myorder::getId, orderId)
                .set(Myorder::getStatus, 1)
                .update();
        model.addAttribute("orderId", orderId);

        return "/ok";


    }

    @Autowired
    OrderDetailService orderDetailService;


    @RequestMapping("/orderDetail")

    public String orderDetail( Integer id,HttpSession session, Model model) {
      List<OrderDetail> orderDetails=orderDetailService.listOrderDeatailById(id);
      Myuser user=(Myuser)session.getAttribute("user");
      int userId=user.getId();
      UserAddress userAddress=userAddressService.lambdaQuery()
                      .eq(UserAddress::getUserId,userId)
                              .one();

      Myorder order=orderService.lambdaQuery()
              .eq(Myorder::getId,id).one();
      if(userAddress!=null){
          String addr=userAddress.getAddress()+" "+userAddress.getName()+" 电话:"+userAddress.getPhone();
          model.addAttribute("userAddress",addr);
      }

        model.addAttribute("orderDetails",orderDetails);
        model.addAttribute("order",order);
        return "/myorder";


    }


}
